﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BrandUnion.CustomData;
using System.Web.UI.HtmlControls;

public partial class Templates_Bookings_Default : BaseTemplate
{
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
    }

    protected void dsDates_Select(object sender, Acms.DataControls.EventDrivenDataSourceSelectEventArgs e)
    {
        var data = BookingSlots.Select(x => x.DateTime.Date).Distinct().ToArray();
        e.SetData(data);
    }

    private List<BrandUnion.CustomData.BookingSlot> _BookingSlots;
    private List<BrandUnion.CustomData.BookingSlot> BookingSlots
    {
        get
        {
            if (_BookingSlots == null)
            {
                _BookingSlots = GetBookingSlotFactory().GetAll();
            }
            return _BookingSlots;
        }
    }

    private BookingFactory _BookingFactory;
    protected BookingFactory GetBookingFactory()
    {
        if (_BookingFactory == null)
        {
            _BookingFactory = new BookingFactory();
        }
        return _BookingFactory;
    }

    private BookingSlotFactory _BookingSlotFactory;
    protected BookingSlotFactory GetBookingSlotFactory()
    {
        if (_BookingSlotFactory == null)
        {
            _BookingSlotFactory = new BookingSlotFactory();
        }
        return _BookingSlotFactory;
    }

    private BrandUnion.CustomData.Booking _CurrentBooking;
    protected BrandUnion.CustomData.Booking CurrentBooking
    {
        get
        {
            if (_CurrentBooking == null && HasBooking == null && User.Identity.IsAuthenticated)
            {
                _CurrentBooking = GetBookingFactory().GetByUserId(User.Identity.User.Id);
                HasBooking = _CurrentBooking != null;
            }
            return _CurrentBooking;
        }
    }

    private bool? HasBooking;

    protected void listTimes_DataBinding(object sender, EventArgs e)
    {
        ListView listTimes = sender as ListView;
        var item = listTimes.NamingContainer as Acms.WebControls.SimpleRepeaterItem;
        if (item != null)
        {
            var date = (DateTime)item.DataItem;
            var data = BookingSlots.Where(x => x.DateTime.Date == date).ToList();

            listTimes.DataSource = data;
        }
    }

    protected void listTimes_ItemDataBound(object sender, ListViewItemEventArgs e)
    {
        if (e.Item.ItemType == ListViewItemType.DataItem)
        {
            BookingSlot slot = ((ListViewDataItem)e.Item).DataItem as BookingSlot;
            Panel pnlItem = e.Item.FindControl("pnlItem") as Panel;
            LinkButton btnItem = e.Item.FindControl("btnItem") as LinkButton;

            if (slot != null && pnlItem != null && btnItem != null)
            {
                if (CurrentBooking != null && slot.Id == CurrentBooking.BookingSlotId)
                {
                    pnlItem.CssClass = "item selected";
                    btnItem.Enabled = false;
                }
                else if (slot.NoSlots <= slot.Booked || slot.DateTime < DateTime.Now)
                {
                    pnlItem.CssClass = "item full";
                    btnItem.Enabled = false;
                }
                else
                {
                    btnItem.Enabled = true;
                }
            }
        }
    }

    protected void listTimes_ItemCommand(object sender, ListViewCommandEventArgs e)
    {
        if (e.CommandName == "SelectDate")
        {
            var bookingSlotId = int.Parse((string)e.CommandArgument);
            BrandUnion.CustomData.Booking booking = CurrentBooking;
            if (booking == null)
            {
                booking = new Booking(User.Identity.User.Id);
                _CurrentBooking = booking;
            }
            booking.BookingSlotId = bookingSlotId;
            GetBookingFactory().Save(booking);
            Acms.Bll.Email email = Acms.Bll.Email.CreateInstance(User.Identity.User, Server.MapPath("~/Emails/JeanGeanieEmail_BrandUnion.xml"));
            email.Format("AppointmentDate", booking.DateTime.ToLongDateString());
            email.Format("AppointmentTime", booking.DateTime.ToShortTimeString());
            email.Send();
            DataBind();
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            SetFlashHeaderVariables(flashHeader);
            if (Request.IsAuthenticated && CurrentBooking != null)
            {
                //set the index of the date to show in the hidden field so accordian picks it up
                var date = CurrentBooking.DateTime.Date;
                var accordianState = loginView.FindControl("accordianState") as HiddenField;
                accordianState.Value = BookingSlots.Select(x => x.DateTime.Date).Distinct().ToList().IndexOf(date).ToString();
            }

            DataBind();
        }
    }
}
